﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BlogEngine.Core
{
    /// <summary>
    /// Class that consists of a Key ID in relationship with a Culture.
    /// </summary>
    /// <typeparam name="T">Type of Key</typeparam>
    public class KeyCulturePair<T>
    {
        /// <summary>
        /// Constructor where Culture is null.
        /// </summary>
        /// <param name="key">ID value</param>
        public KeyCulturePair(T key)
        {
            _key = key;
        }

        /// <summary>
        /// Constructor where Culture is specified as string data type.
        /// </summary>
        /// <param name="key">ID value</param>
        /// <param name="culture">Culture value</param>
        public KeyCulturePair(T key, string culture)
            : this(key, BlogSettings.Instance.CultureFrontend[culture]) { }

        /// <summary>
        /// Constructor where Culture is specified as byte data type (equals SQL tinyint data type).
        /// </summary>
        /// <param name="key">ID value</param>
        /// <param name="culture">Culture value</param>
        public KeyCulturePair(T key, byte culture)
            : this(key, BlogSettings.Instance.CultureFrontend[culture]) { }

        /// <summary>
        /// Constructor where Culture is specified as <see cref="T:BlogEngine.Core.CultureCollectionItem"/> type.
        /// </summary>
        /// <param name="key">ID value</param>
        /// <param name="culture">Culture value</param>
        public KeyCulturePair(T key, CultureCollectionItem culture)
        {
            _culture = culture;
            _key = key;
        }

        private T _key;
        /// <summary>
        /// Gets ID value.
        /// </summary>
        public T Key
        {
            get { return _key; }
        }

        private CultureCollectionItem _culture;
        /// <summary>
        /// Gets culture information related to this ID.
        /// </summary>
        public CultureCollectionItem Culture
        {
            get { return _culture; }
        }
        
        /// <summary>
        /// Compares two objects of <see cref="T:BlogEngine.Core.KeyCulturePair"/> type for equality.
        /// </summary>
        /// <param name="item1">First object</param>
        /// <param name="item2">Second object</param>
        /// <returns>Returns true if these objects are equal, otherwise false.</returns>
        public static bool operator ==(KeyCulturePair<T> item1, KeyCulturePair<T> item2)
        {
            if ((object)item1 != null && (object)item2 != null && item1.Key.Equals(item2.Key) && item1.Culture.CultureID == item2.Culture.CultureID)
                return true;
            else if ((object)item1 == null && (object)item2 == null)
                return true;
            else
                return false;
        }

        /// <summary>
        /// Compares two objects of <see cref="T:BlogEngine.Core.KeyCulturePair"/> type for inequality.
        /// </summary>
        /// <param name="item1">First object</param>
        /// <param name="item2">Second object</param>
        /// <returns>Returns true if these objects are not equal, otherwise false.</returns>
        public static bool operator !=(KeyCulturePair<T> item1, KeyCulturePair<T> item2)
        {
            if ((object)item1 != null && (object)item2 != null && item1.Key.Equals(item2.Key) && item1.Culture.CultureID == item2.Culture.CultureID)
                return false;
            else if ((object)item1 == null && (object)item2 == null)
                return false;
            else
                return true;
        }

        /// <summary>
        /// Compares this object with input object for equality.
        /// </summary>
        /// <param name="obj">Input object</param>
        /// <returns>Returns true if input object is equal, otherwise false.</returns>
        public override bool Equals(object obj)
        {
            if (obj != null && obj is KeyCulturePair<T>)
            {
                KeyCulturePair<T> objPair = (KeyCulturePair<T>)obj;
                if (objPair.Key.Equals(Key) && objPair.Culture.CultureID == Culture.CultureID)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Server as a hash function for this type.
        /// </summary>
        /// <returns>Returns hash.</returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Convert current object to string.
        /// </summary>
        /// <returns>Returns object's Key (ID) value.</returns>
        public override string ToString()
        {
            return Key.ToString();
        }
    }
}
